#!/bin/bash

# 定义路径变量
CURRENT_DIR="$(pwd)"
JAR_FILE="mysql_tuning.jar"
JMX_AGENT="${CURRENT_DIR}/jmx_exporter/jmx_prometheus_javaagent-1.3.0.jar"
JMX_CONFIG="${CURRENT_DIR}/jmx_exporter/config.yaml"

# 停止已存在的进程
pkill -f "${JAR_FILE}" || true

# 启动应用
nohup java -Xms512m -Xmx512m \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath="${CURRENT_DIR}/" \
-XX:+PrintGCDetails \
-XX:+PrintGCDateStamps \
-Xloggc:"${CURRENT_DIR}/gc.log" \
-javaagent:"${JMX_AGENT}"=9090:"${JMX_CONFIG}" \
-jar "${CURRENT_DIR}/${JAR_FILE}" > "${CURRENT_DIR}/app.log" 2>&1 &

echo "应用已启动，PID: $!"
echo "当前工作目录: ${CURRENT_DIR}"
echo "堆转储将保存到: ${CURRENT_DIR}/"
echo "日志文件: ${CURRENT_DIR}/app.log"
echo "GC日志: ${CURRENT_DIR}/gc.log"
